package com.example.startapp;

//管理员登录验证


import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class AdministratorActivity extends AppCompatActivity {

    Button btn_login;
    EditText text_adminame;
    EditText text_adminpsw;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_administrator);

        text_adminame=findViewById(R.id.adminN);
        text_adminpsw=findViewById(R.id.adminpsw);
        btn_login=findViewById(R.id.login_admin);
        btn_login.setOnClickListener(v -> adminLogin(text_adminame.getText().toString(),text_adminpsw.getText().toString()));
    }


private void adminLogin(String username, String password) {
    OkHttpClient client = new OkHttpClient();
    String json = "{\"userName\":\"" + username + "\",\"userPwd\":\"" + password + "\"}";
    RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), json);

    Request request = new Request.Builder()
            .url("http://192.168.239.136:8888/api/admin/adminlogin")
            .post(body)
            .build();

    client.newCall(request).enqueue(new okhttp3.Callback() {
        @Override
        public void onFailure(okhttp3.Call call, IOException e) {
            runOnUiThread(() -> Toast.makeText(AdministratorActivity.this, "登录失败1: " + e.getMessage(), Toast.LENGTH_SHORT).show());
        }

        @Override
        public void onResponse(okhttp3.Call call, Response response) throws IOException {
            if (!response.isSuccessful()) {
                runOnUiThread(() -> {
                    Toast.makeText(AdministratorActivity.this, "登录失败2: " + response.code(), Toast.LENGTH_SHORT).show();
                    // 提示用户检查网络连接或稍后再试
                    Log.e("LoginResponse", "Response Code: " + response.code() + " Message: " + response.message());
                    Toast.makeText(AdministratorActivity.this, "请检查网络连接或稍后再试", Toast.LENGTH_SHORT).show();
                });
                return;
            }
            String responseBody = response.body().string();
            Log.e("response", responseBody);
            if (responseBody.isEmpty()) {
                runOnUiThread(() -> Toast.makeText(AdministratorActivity.this, "登录失败: 用户名或密码错误", Toast.LENGTH_SHORT).show());
            } else {
                runOnUiThread(() -> {
                    Toast.makeText(AdministratorActivity.this, "登录成功: " + responseBody, Toast.LENGTH_SHORT).show();
                    // 跳转到管理员界面
                    Intent intent=new Intent();
                intent.setAction("admin.adminActivity");
                intent.addCategory(Intent.CATEGORY_DEFAULT);
                startActivity(intent);
                });
            }
        }
    });
}
}